﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Net.Fan.SmallPig.DataBase;

namespace Net.Fan.SmallPig.DataAccess
{
    public class SystemSettingDA
    {
        public static DataRow GetSetting(string settingName)
        {
            string sql = "select * from SystemSetting where Name = @Name";

            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@Name", settingName);

            DataSet ds = DA_Base.RunReturnDataset(sql, paras);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }

        public static void SetSetting(string name, string val)
        {
            string sql = "Update SystemSetting set Value = @Value  where Name = @Name";

            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@Name", name);
            paras[1] = new SqlParameter("@Value", val);
            if (DA_Base.ExecuteNonQuery(sql, paras) <= 0)
            {
                //no exist user setting
                InsertSetting(name, val);

            }
        }

        private static void InsertSetting(string name, string val)
        {
            string sql = "Insert SystemSetting (Name,Value) values (@Name,@Value)";

            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@Name", name);
            paras[1] = new SqlParameter("@Value", val);

            DA_Base.ExecuteNonQuery(sql, paras);
        }
    }
}
